package com.ocn.li.scmdy.modules.app.service.impl;


import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ocn.li.scmdy.common.Enum.ChannelCodeEnum;
import com.ocn.li.scmdy.common.exception.MyException;
import com.ocn.li.scmdy.modules.app.dao.UserDao;
import com.ocn.li.scmdy.modules.app.entity.CoinDetailEntity;
import com.ocn.li.scmdy.modules.app.entity.UserEntity;
import com.ocn.li.scmdy.modules.app.form.UpdateCoinForm;
import com.ocn.li.scmdy.modules.app.service.CoinDetailService;
import com.ocn.li.scmdy.modules.app.service.UserService;
import com.ocn.li.scmdy.common.utils.QueryExt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * TODO UserService 实现类
 *
 * @author crazypenguin
 * @version 1.0.0
 * @createdate 2019/1/9
 */
@Service("userService")
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private CoinDetailService coinDetailService;

    @Override
    public UserEntity queryByMobile(String mobile) {
        UserEntity userEntity = new UserEntity();
        userEntity.setMobile(mobile);
        return baseMapper.selectOne(userEntity);
    }

    @Override
    public Page selectPageByConditions(QueryExt query) {
        Map map = new HashMap();
        map.putAll(query.getCondition());
        query.setRecords(userDao.selectPageByConditions(query, map));
        return query;
    }


    @Override
    @Transactional
    public void register(UserEntity user) {

        // 保存进用户表
        userDao.insert(user);
    }

    @Override
    public Integer updateCoin(UpdateCoinForm form) throws MyException {
        CoinDetailEntity coinDetail  = new CoinDetailEntity();
        coinDetail.setChannelCode(ChannelCodeEnum.THREE_EYE_FROG.getValue());
        coinDetail.setChannelName(ChannelCodeEnum.THREE_EYE_FROG.getName());
        coinDetail.setSerialNumber(RandomUtil.randomNumbers(16));
        coinDetail.setCreateTime(new Date());
        coinDetail.setCoin(form.getNum());
        coinDetail.setDesc(form.getDesc());
        coinDetail.setMobile(form.getMobile());
        coinDetail.setTerminal("server");
        coinDetail.setNickname(form.getMobile());
        if(form.getType()==1){
            coinDetail.setMark("后门充值");
            Integer integer = this.baseMapper.subCoinByMobile(form.getMobile(), (0 - form.getNum()));
            if(integer!=1){
                throw new MyException("增加失败");
            }
            coinDetail.setType("获得");
            coinDetailService.insert(coinDetail);
            return 1;
        }else if(form.getType()==0){
            coinDetail.setMark("后门扣除");
            Integer integer = this.baseMapper.subCoinByMobile(form.getMobile(), form.getNum());
            if(integer!=1){
                throw new MyException("扣除失败，请查看余额");
            }
            coinDetail.setType("花费");
            coinDetailService.insert(coinDetail);
            return 0;
        }
        return null;
    }



}
